﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NOSDC.Areas.Admin.Models;
using NOSDC.Pocos;
using NOSDC.Services;
using NOSDC.Repositories;
using NOSDC.Services.Extensions;

namespace NOSDC.Areas.Admin.Controllers
{
    public class EventsController : BaseController
    {
        private readonly EventService _service;
        private readonly SponsorRepository _repository;
        private readonly SpeakerRepository _speakerRepository;

        public EventsController()
        {
            _service = new EventService();
            _repository = new SponsorRepository();
            _speakerRepository = new SpeakerRepository();
        }

        // GET: /Admin/Events/
        public ActionResult Index()
        {
            var model = new EventsViewModel { Events = _service.GetAllEvents() };
            foreach (var m in model.Events)
                m.Description = m.Description.StripHTML();
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new EventsCreateViewModel
                            {
                                Sponsor = _repository.GetAllSponsors().ToList(),
                                Speaker = _speakerRepository.GetAll().ToList()
                            };

            return View(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(EventsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                _service.CreateEvent(model.Events, model.SponsorCheckList.Where(s => s.Checked).Select(s => s.ID).ToList(), model.SpeakerCheckList.Where(s => s.Checked).Select(s => s.ID).ToList());
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Update(int id)
        {
            var model = new EventsCreateViewModel
                            {
                                Events = _service.GetEventByID(id),
                                Sponsor = _repository.GetAllSponsors().ToList(),
                                Speaker = _speakerRepository.GetAll().ToList()
                            };

            return View(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Update(int id, EventsCreateViewModel model)
        {
            _service.UpdateEvent(model.Events, model.SponsorCheckList.Where(s => s.Checked).Select(s => s.ID).ToList(), model.SpeakerCheckList.Where(s => s.Checked).Select(s => s.ID).ToList());
            return RedirectToAction("Index");
        }

        #region Finalize Events

        public ActionResult Finalize(int? id, bool isFinal)
        {
            var model = isFinal ? new FinalizeEventModel {FinalAttendees = _service.GetFinalAttendees(id.Value)} : new FinalizeEventModel {Attendees = _service.GetAttendees(id.Value)};
            return View(model);
        }

        [HttpPost]
        public ActionResult Finalize(FinalizeEventModel model, int id)
        {
           if(model != null)
           {
               if(ModelState.IsValid)
               {
                   var final = model.AttendeesCheckList.Where(e => e.Checked).Select(i => new FinalEventAttendee {EventId = id, UserId = i.ID}).ToList();
                   _service.FinalizeEvent(final, id);
               }
           }
            return View(model);
        }

        public ActionResult PrintAttendees(int id)
        {
            var repo = new UserDetailsRepository();
            var model = new AttendeeViewModel { Users = new List<UserDetail>(), EventName = _service.GetEventByID(id).Title };

            foreach(var attendee in _service.GetAttendees(id))
            {
                model.Users.Add(repo.GetUserDetails(attendee.UserID));
            }

            return View(model);
        }

        #endregion
    }
}
